package com.usian.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usian.common.R;
import com.usian.mapper.DeptMapper;
import com.usian.mapper.PostsMapper;
import com.usian.pojo.Dept;
import com.usian.pojo.Emp;
import com.usian.pojo.Posts;
import com.usian.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/dept")
public class DeptController {
    @Autowired
    private DeptService deptService;

    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private PostsMapper postsMapper;

    /**
     * 查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R findPage(@RequestParam(defaultValue = "1")Integer page, @RequestParam(defaultValue = "5")Integer pageSize, String name){
        Page<Dept> page1=deptService.findPage(page,pageSize,name);
        return R.success(page1);
    }

    /**
     * 添加
     * @param dept
     * @return
     */
    @PostMapping
    public R add(@RequestBody Dept dept){
        Integer count1=deptMapper.findByName(dept.getName());
        if (count1>0) {
            return R.error("该部门已存在！");
        }
        return deptService.add(dept);
    }

    /**
     * 查询全部信息
     * @return
     */
    @GetMapping("findByDept")
    public R list2(HttpSession session){
        List<Dept> list=deptService.list2(session);
        return R.success(list);
    }

    /**
     * 回显
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public R findById(@PathVariable Integer id){
        Dept dept=deptMapper.selectById(id);
        return R.success(dept);
    }

    /**
     * 修改
     * @param dept
     * @param session
     * @return
     */
    @PutMapping
    public R edit(@RequestBody Dept dept,HttpSession session){
        String name=deptMapper.findByName1(dept.getId());
        Integer count1=deptMapper.findCount(dept.getId());
        if (!dept.getName().equals(name)){
            if (count1!=0){
                return R.error("该部门有员工，无法直接修改！");
            }
        }
        Integer count2=deptMapper.findByName2(dept.getId(),dept.getName());
        if(count2!=0){
            return R.error("该部门已存在！");
        }
        deptMapper.updateById(dept);
        return R.success("修改成功");
    }

    /**
     * 批删-单删
     * @param ids
     * @return
     */
    @DeleteMapping
    public R delete(String[] ids){
        QueryWrapper<Posts> queryWrapper=new QueryWrapper<>();
        for (String id : ids) {
            Integer count=deptMapper.findCount(Integer.valueOf(id));
            if (count>0){
                return R.error("所选部门含有员工，无法直接删除！");
            }
            queryWrapper.lambda().eq(Posts::getDid,Integer.valueOf(id));
            postsMapper.delete(queryWrapper);
        }
        deptMapper.deleteBatchIds(Arrays.asList(ids));
        return R.success("删除成功");
    }
}



















